//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////
// configure the map here
//////////////////////////////////////////////////////////////////////////////////
function initMap() {

	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		//getExtents();
		map.enableDoubleClickZoom();
		map.setCenter(new GLatLng(g_centerLatitude, g_centerLongitude), g_startZoom);
		map.setMapType(G_HYBRID_MAP);
	} else {
		alert("It appears that the browswer you are using is not compatible with Goolge Maps. We recommend Internet Explorer or Firefox.");	
	}
}



/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// this routine figures out the center point lat/long and the zoom level
// based on the points to be displayed on the map
// 
// not currently used because there are too many cases where our arbitrary choices for the zoom level hide points we want to see.
/////////////////////////////////////////////////////////////////////////////
function getExtents() {
	
	var pointFarthestWest;
	var pointFarthestEast; 
	var pointFarthestNorth;
	var pointFarthestSouth; 
	
	// if we have no markers return. This means that the default values set above are used
	if (markers.length == 0) {
		g_startZoom = 3;
		return;
	}

	if (markers.length == 1) {
		g_centerLatitude = markers[0].latitude;
		g_centerLongitude = markers[0].longitude;
		g_startZoom = g_defaultZoom;
		return;
	}

	pointFarthestWest = markers[0].longitude;
	pointFarthestEast = markers[0].longitude;
	pointFarthestNorth = markers[0].latitude;
	pointFarthestSouth = markers[0].latitude;

	for (i=1; i < markers.length; i ++) {
		if (Math.abs(markers[i].longitude) > Math.abs(pointFarthestWest)) {
			pointFarthestWest = markers[i].longitude;
		}
	}
	
	for (i=1; i < markers.length; i ++) {
		if (Math.abs(markers[i].longitude) < Math.abs(pointFarthestEast)) {
			pointFarthestEast = markers[i].longitude;
		}
	 }

	for (i=1; i < markers.length; i ++) {
		if (Math.abs(markers[i].latitude) > Math.abs(pointFarthestNorth)) {
			pointFarthestNorth = markers[i].latitude;
		}
	 }

	for (i=1; i < markers.length; i ++) {
		if (Math.abs(markers[i].latitude) < Math.abs(pointFarthestSouth)) {
			pointFarthestSouth = markers[i].latitude;
		}
	 }

	if (pointFarthestWest >0) {signFW = "pos";} else {signFW = "neg";};
	if (pointFarthestEast >0) {signFE = "pos";} else {signFE = "neg";};
	if (pointFarthestNorth >0) {signFN = "pos";} else {signFN = "neg";};
	if (pointFarthestSouth >0) {signFS = "pos";} else {signFS = "neg";};
	
	if (signFW == signFE) {
		var distanceBetweenLong = Math.abs(Math.abs(pointFarthestWest) - Math.abs(pointFarthestEast));
		if (signFW == "neg") {
			var longStartPoint = pointFarthestEast - (distanceBetweenLong/2);
		} else {
			longStartPoint = pointFarthestEast + (distanceBetweenLong/2);
		}
		//if (signFW == "neg") { longStartPoint = longStartPoint*-1;};
	} else {
		var distanceBetweenLong = Math.abs(Math.abs(pointFarthestWest) + Math.abs(pointFarthestEast));
		var longStartPoint = Math.max(Math.abs(pointFarthestEast), Math.abs(pointFarthestWest)) - (distanceBetweenLong/2);
	}

	if (signFN == signFS) {
		var distanceBetweenLat = Math.abs(Math.abs(pointFarthestNorth) - Math.abs(pointFarthestSouth));
		if (signFW == "neg") {
			var latStartPoint = pointFarthestNorth - (distanceBetweenLat/2);
		} else {
			var latStartPoint = pointFarthestSouth + (distanceBetweenLat/2);
		}
	} else {
		var distanceBetweenLat = Math.abs(Math.abs(pointFarthestNorth) + Math.abs(pointFarthestSouth));
		var latStartPoint = Math.max(Math.abs(pointFarthestNorth), Math.abs(pointFarthestSouth)) - (distanceBetweenLat/2);
	}
	
	//var longStartPoint = Math.max(Math.abs(pointFarthestEast), Math.abs(pointFarthestWest)) - (distanceBetweenLong/2);
	//var latStartPoint = Math.max(Math.abs(pointFarthestNorth), Math.abs(pointFarthestSouth)) - (distanceBetweenLat/2);
	
	var distanceToZoomby = Math.max(distanceBetweenLong, distanceBetweenLat);	

	if (distanceToZoomby < 4) {
		startZoomLevel = 8;
	} else if (distanceToZoomby >= 4 & distanceToZoomby < 12) {
		startZoomLevel = 6;
	} else if (distanceToZoomby >= 12 & distanceToZoomby < 30) {
		startZoomLevel = 5;
	} else if (distanceToZoomby >= 12 & distanceToZoomby < 30) {
		startZoomLevel = 5;
	} else if (distanceToZoomby >= 30 & distanceToZoomby < 100) {
		startZoomLevel = 3;
		
	} else {
		startZoomLevel = 2;
	}
	g_centerLatitude = latStartPoint;
	g_centerLongitude = longStartPoint;
	if (g_startZoom < 8) {
		g_startZoom = startZoomLevel;
	}
}


/////////////////////////////////////////////////////////////////////////////////////
// used by the window resizing code
/////////////////////////////////////////////////////////////////////////////////////
function windowWidth() {
	if (self.innerWidth) {
		return self.innerWidth;
	}
		
	if (document.documentElement && document.documentElement.clientWidth) {
		return y = document.documentElement.clientWidth;
	}
	
	if (document.body) {
		return document.body.clientWidth;
	}
	
	// just in case
	return 0;
	
}



/////////////////////////////////////////////////////////////////////////////////////
// used by the window resizing code
/////////////////////////////////////////////////////////////////////////////////////
function windowHeight() {
	if (self.innerHeight) {
		return self.innerHeight;
	}
		
	if (document.documentElement && document.documentElement.clientHeight) {
		return y = document.documentElement.clientHeight;
	}
	
	if (document.body) {
		return document.body.clientHeight;
	}
	
	// just in case
	return 0;
	
}

////////////////////////////////////////////////////////////////////////////////////////
// resize the panes (including the map) that make up the standard application layout
////////////////////////////////////////////////////////////////////////////////////////
function handleResize() {
	
	var centerLat = map.getCenter();
	var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 30;
	var width = windowWidth() - document.getElementById('sidebar').offsetWidth - 30;
	document.getElementById('map').style.height = height + 'px';
	document.getElementById('map').style.width = width + 'px';
	document.getElementById('sidebar').style.height = height + 'px';
		
	// I cound not get detection of the iframe to work in a crossbrowser 
	// compatible way so we pass in the variable. Which is a bit of a hack
	tempObj = document.getElementById('sidebar_frame');
	if (tempObj == null) {
		tempObj = document.getElementById('add_locs_block');
	}
	if (tempObj != null) {
		
		tempObj.style.height = height + 'px';
	}
	
	map.checkResize();
	map.setCenter(centerLat);
}

///////////////////////////////////////////////////////////////////////////////////////
// NOTE!!! This does not work correctly with all possible object types
function isDefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
function gotoPage(pageName) {

	createCookie("category", g_category, 180);
	createCookie("sec", g_section, 180);
	createCookie("lng", map.getCenter().x, 180);
	createCookie("lat", map.getCenter().y, 180);
	var temp = map.getZoom();
	createCookie("zoom_level", temp, 180);
	
	var pagestr = pageName + "?category=" + g_category + "&sec=" + g_section; 
	window.location= pagestr;	
}


///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
function validURL(theURL) {
	var theSubStr = theURL.substr(0, 4);
	
	if (theSubStr.toUpperCase() != "HTTP") {
		//GLog.write("returning false");
		return false;	
	} else {
		//GLog.write("returning true");
		return true;
	}
		
}


/////////////////////////////////////////////////////////////////////////////////
// event handler associated with the marker when added to the map
function getPageByClick2(theUrl) {
	currentPage = theUrl;
	openPage();
}



